package per.brant.template.snippet

/**
 * Created by brantyan on 14-1-16.
 */
import net.liftweb.util._
import Helpers._
import net.liftweb.http.{TransientRequestVar, SHtml}
import net.liftweb.http.js.JE._
import net.liftweb.http.js.JsCmds._
import net.liftweb.http.js.JsCmd
import per.brant.template.service.{Pony, PonyService}

class PonySnippet {

  object pony extends TransientRequestVar[Pony](dflt = PonyService.getRandomPony.openOrThrowException("not found best pony!"))

  def initPony = ".name"

  def renderText= <p>Twilight sparkle is best pony!</p>

  def renderImage = "img [src]" #> "/images/ajax-loader.gif" &
                    "img [alt]" #> "Twilight sparkle"

  def renderButton ="button [onclick]" #> SHtml.ajaxInvoke(()=>theBigReveal)

  def theBigReveal:JsCmd = {
      JsRaw(
        """
          |var div =$('#ponyDiv');
          |div.show();
          |div.find('p').text("%s is best pony");
          |div.find('img').attr('src','%s');
          |
        """.stripMargin.format(pony.name,pony.imageUrl))

    }
}
